package com.six.epidemic.admin.service.impl

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.six.epidemic.admin.entity.EpidemicAdmin
import com.six.epidemic.admin.mapper.EpidemicAdminMapper
import com.six.epidemic.admin.service.IEpidemicAdminService
import org.springframework.stereotype.Service
import java.util.*
import javax.annotation.Resource

@Service
class EpidemicAdminService : IEpidemicAdminService {
    @Resource
    private lateinit var mapper: EpidemicAdminMapper


    override fun ckUsername(username: String): EpidemicAdmin? {
        //设置查询条件
        val queryWrapper = QueryWrapper<EpidemicAdmin>()
        queryWrapper.eq("username", username)
        return mapper.selectOne(queryWrapper)
    }

    override fun add(admin: EpidemicAdmin): Int {
        admin.userpwd = Md5Utils.md5(admin.userpwd)
        // 用户状态为正常
        admin.ustate = 1
        admin.regtime = Date()
        // 添加的用户为一般管理员
        admin.utype = 1
        // 二次数据验证
        return if (ckUsername(admin.username) != null) 0 else mapper.insert(admin)
    }

    override fun update(admin: EpidemicAdmin): Int {
        // 添加的用户为一般管理员
        admin.lastlogintime = Date()
        return mapper.updateById(admin)
    }

    override fun getList(provinces: String): List<EpidemicAdmin> {
        val queryWrapper: QueryWrapper<EpidemicAdmin> = QueryWrapper<EpidemicAdmin>()
        // 非超级管理员列表
        queryWrapper.eq("utype", 1)
        queryWrapper.like("provinces", provinces)
        return mapper.selectList(queryWrapper)
    }
}